package com.baosight.sczx.common.zm.cInterface.cp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanFC;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanGC;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanGLAZ;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanJD;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanQY;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanWM;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanYT;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZC;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZD;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZK;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZL;

public class ProducePlanOther {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 验证是否存在记录
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean validate(Map<String, String> params, String sql) {
		if (params == null || sql == null) {
			return true;
		}
		
		List result = dao.query(sql, params);
		if(result != null && result.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 专业计划上报成功
	 * 复制明细记录
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void clone(String fdProduceplanId, String fdProfessionalplanId, String tableName, String querySql, String insertSql) {
		if(fdProduceplanId != null && !fdProduceplanId.trim().equals("") && fdProfessionalplanId != null && !fdProfessionalplanId.trim().equals("")) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			_map.put("isProfessionalplan", "任意数值");
			
			List result = dao.query(querySql, _map);
			for(int i = 0; i < result.size(); i ++) {
				Map _bean = (HashMap)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue(tableName);
				_bean.put("fdKeyId", fdKeyId);
				_bean.put("fdProduceplanId", fdProduceplanId);
				
				dao.insert(insertSql, _bean);
			}
		}
	}
	
	/**
	 * 生产计划调整时，复制采煤、掘进之外的其他计划
	 * @param fdProduceplanId
	 */
	@SuppressWarnings("rawtypes")
	public static void cloneByAdjust(String oldId, String newId) {
		if(oldId != null && newId != null) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProduceplanId", oldId);
			
			//抽采-钻尺
			List result = dao.query("ZMCPProducePlanZC.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanZC _plan = (ZMCPProducePlanZC)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZC");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanZC.insert", _plan.toMap());
			}
			
			//抽采-钻孔
			result = dao.query("ZMCPProducePlanZK.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanZK _plan = (ZMCPProducePlanZK)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZK");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanZK.insert", _plan.toMap());
			}
			
			//抽采-管路安装
			result = dao.query("ZMCPProducePlanGLAZ.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanGLAZ _plan = (ZMCPProducePlanGLAZ)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_GLAZ");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanGLAZ.insert", _plan.toMap());
			}
			
			//一通三防
			result = dao.query("ZMCPProducePlanYT.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanYT _plan = (ZMCPProducePlanYT)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_YT");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanYT.insert", _plan.toMap());
			}
			
			//机电
			result = dao.query("ZMCPProducePlanJD.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanJD _plan = (ZMCPProducePlanJD)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_JD");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanJD.insert", _plan.toMap());
			}
			
			//粉尘
			result = dao.query("ZMCPProducePlanFC.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanFC _plan = (ZMCPProducePlanFC)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_FC");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanFC.insert", _plan.toMap());
			}
			
			//质量
			result = dao.query("ZMCPProducePlanZL.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanZL _plan = (ZMCPProducePlanZL)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZL");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanZL.insert", _plan.toMap());
			}
			
			//全岩找顶
			result = dao.query("ZMCPProducePlanQY.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanQY _plan = (ZMCPProducePlanQY)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_QY");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanQY.insert", _plan.toMap());
			}
			
			//重节点
			result = dao.query("ZMCPProducePlanZD.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanZD _plan = (ZMCPProducePlanZD)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZD");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanZD.insert", _plan.toMap());
			}
			
			//规程设计
			result = dao.query("ZMCPProducePlanGC.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanGC _plan = (ZMCPProducePlanGC)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_GC");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanGC.insert", _plan.toMap());
			}
			
			//文明生产
			result = dao.query("ZMCPProducePlanWM.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanWM _plan = (ZMCPProducePlanWM)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_WM");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanWM.insert", _plan.toMap());
			}
		}
	}
	
	/**
	 * 专业计划退回时，删除数据
	 * @param oldId
	 * @param newId
	 */
	public static int goback(String fdProfessionalplanId, String sql) {
		if(fdProfessionalplanId != null && sql != null) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			return dao.delete(sql, _map);
		}
		return -1;
	}
}
